package controller;

import java.sql.ResultSet;
import java.util.LinkedList;
import misc.Configurations;
import misc.Logger;
import data.DatabaseGateway;
import data.DownloadLog;
import data.UploadLog;

public class LogViewController {
    public LinkedList<DownloadLog> retrieveDownloadLog(long fileID){
        try{
            String q="SELECT * FROM DOWNLOADLOGS WHERE FileID='"+fileID+"'";
            DatabaseGateway data = new DatabaseGateway();
            data.openConnection(Configurations.User,Configurations.Password,
                                    Configurations.DatabaseName);

            //Querying
            ResultSet rs_download = data.executeQuery(q);

            //Convert results to interactable items
            LinkedList<DownloadLog> downloads= new LinkedList<DownloadLog>();
            while(rs_download.next()){
                DownloadLog new_download = new DownloadLog();
                new_download.setIPaddress(rs_download.getString("IP"));
                new_download.setStart(rs_download.
                                            getTimestamp("StartTimeDate"));
                new_download.setFinish(rs_download.
                                            getTimestamp("FinishTimeDate"));
                new_download.setUsername(rs_download.getString("Username"));
                downloads.add(new_download);
            }
            
            data.closeConnection();
            return downloads;
        }catch(Exception e){
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return null;
    }

    public UploadLog retrieveUploadLog(long fileID){
        try{
            String query = "SELECT *,FILES.OriginalName as 'filename',"+
                        "FILES.Size as 'filesize',FILES.Type as 'filetype',"+
                        "FILES.isProtected as 'fileProtected',"+
                        "FILES.HashedPassword as 'hpassword' FROM UPLOADLOGS,"+
                        "FILES WHERE UPLOADLOGS.FileID='"+fileID+"' and "+
                        "UPLOADLOGS.FileID=FILES.FileID";

            DatabaseGateway data = new DatabaseGateway();
            data.openConnection(Configurations.User,Configurations.Password,
                                    Configurations.DatabaseName);
            int numRows=data.executeCount(query);

            if(numRows<=0)
                return null;

            //Querying
            ResultSet rs_upload = data.executeQuery(query);

            //Convert results to interactable items
            UploadLog new_upload = new UploadLog();
            rs_upload.next();
            new_upload.setFileName(rs_upload.getString("filename"));
            new_upload.setFilesize(rs_upload.getLong("filesize"));
            new_upload.setFiletype(rs_upload.getString("filetype"));
            new_upload.setIPaddress(rs_upload.getString("IP"));
            new_upload.setStart(rs_upload.getTimestamp("StartTimeDate"));
            new_upload.setFinish(rs_upload.getTimestamp("FinishTimeDate"));
            new_upload.setFileIsProtected(rs_upload.
                                            getBoolean("fileProtected"));
            new_upload.setUsername(rs_upload.getString("Username"));
            new_upload.setFileHashedPassword(rs_upload.getString("hpassword"));
            data.closeConnection();
            return new_upload;

        }catch(Exception e){
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return null;
    }

    public long retrieveFileID(String hashedcode)
    {
        try
        {
            String query = 
                    "SELECT * FROM FILES WHERE HashedCode='"+hashedcode+"'";

            DatabaseGateway gw = new DatabaseGateway();
            gw.openConnection(Configurations.User,Configurations.Password,
                                        Configurations.DatabaseName);
            int numRows=gw.executeCount(query);

            if(numRows<=0)
                return -1;

            ResultSet rs = gw.executeQuery(query);

            long id=-1;
            rs.next();id=rs.getLong("FileID");
            gw.closeConnection();

            return id;
        }
        catch(Exception e)
        {
            Logger.write(e.getMessage(),Logger.LogType.error);
        }

        return -1;
    }
}

